/**
 * 
 */
package com.sc.softmgr.common.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.acc.softmgr.model.SoftmgrCategory;
import com.acc.softmgr.model.SoftmgrSubSystem;
import com.alibaba.fastjson.JSONObject;
import com.sc.softmgr.business.service.ISoftmgrCategoryFeignService;
import com.sc.softmgr.business.service.ISoftmgrSubSystemFeignService;

/**
 * 
 * @content 缓存对象 在列表展示的时候会经常调用远程服务拿到对象,缓存起来增加效率 ;用完后把缓存删除是为了保持数据的实时性;
 *          单例模式是避免删除竞争同个map数据
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Component
public class CacheObjectComponent {

	private static Map<Integer, SoftmgrCategory> categoryMap = new HashMap<Integer, SoftmgrCategory>();

	private static Map<Integer, SoftmgrSubSystem> subSystemMap = new HashMap<Integer, SoftmgrSubSystem>();

	@Autowired
	private ISoftmgrCategoryFeignService softmgrCategoryFeignService;

	@Autowired
	private ISoftmgrSubSystemFeignService softmgrSubSystemFeignService;

	public SoftmgrCategory getSoftmgrCategory(Integer id) {
		SoftmgrCategory softmgrCategory = categoryMap.get(id);
		if (softmgrCategory == null) {
			JSONObject json = softmgrCategoryFeignService.searchSoftmgrCategory(id);
			if (!"500".equals(json.get("status"))) {
				softmgrCategory = json.getObject("data", SoftmgrCategory.class);
				categoryMap.put(id, softmgrCategory);
				softmgrCategory = categoryMap.get(id);
			}
		}
		return softmgrCategory;
	}

	public void destroyCategoryMap() {
		categoryMap.clear();
	}

	public SoftmgrSubSystem getSubSystem(Integer id) {
		SoftmgrSubSystem softmgrSubSystem = subSystemMap.get(id);
		if (softmgrSubSystem == null) {
			JSONObject json = softmgrSubSystemFeignService.searchSoftSubSystem(id);
			if (!"500".equals(json.get("status"))) {
				softmgrSubSystem = json.getObject("data", SoftmgrSubSystem.class);
				subSystemMap.put(id, softmgrSubSystem);
				softmgrSubSystem = subSystemMap.get(id);
			}
		}
		return softmgrSubSystem;
	}

	public void destroySubSystemMap() {
		subSystemMap.clear();
	}

}
